<template>
  <div class="login-container">
    <el-form
      ref="loginForm"
      :model="loginForm"
      :rules="loginRules"
      class="login-form"
      auto-complete="on"
      label-position="left"
    >

      <div class="title-container">
        <h3 class="title"><img src="../../assets/common/loginlogo.png" alt=""></h3>
      </div>

      <el-form-item prop="loginName">
        <span class="svg-container">
          <i class="el-icon-mobile" />
        </span>
        <el-input
          ref="loginName"
          v-model="loginForm.loginName"
          placeholder="请输入账号"
          name="loginName"
          type="text"
          tabindex="1"
          auto-complete="on"
        />
      </el-form-item>

      <el-form-item prop="password">
        <span class="svg-container">
          <i class="el-icon-lock" />
        </span>
        <el-input
          :key="passwordType"
          ref="password"
          v-model="loginForm.password"
          :type="passwordType"
          placeholder="请输入密码"
          name="password"
          tabindex="2"
          auto-complete="on"
        />
        <span class="show-pwd" @click="showPwd">
          <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
        </span>
      </el-form-item>
      <!-- 验证码 -->
      <el-form-item class="yanzhengma" prop="code">
        <span class="svg-container">
          <svg-icon icon-class="yanzhengyanzhengma" />
        </span>
        <el-input
          ref="code"
          v-model="loginForm.code"
          placeholder="请输入验证码"
          name="code"
          type="text"
          tabindex="1"
          auto-complete="on"
          @keyup.enter.native="logindemo"
        />
        <img class="yanzhengmaImg" :src="`https://likede2-java.itheima.net/api/user-service/user/imageCode/${clientToken}`" alt="" @click="getyanzhengma">
      </el-form-item>

      <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;" @click.native.prevent="logindemo">登录</el-button>

      <div class="tips">
        <span> 仅用于IT培训教学使用，为保障您的个人信息安全，请勿向平台录入任何个人敏感信息（如手机号、身份证号等）！</span>
      </div>

    </el-form>
  </div>
</template>

<script>
import { loginUser } from '@/api/user'
import Cookies from 'js-cookie'

export default {
  name: 'Login',
  data() {
    const validateloginName = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入账号'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'))
      } else {
        callback()
      }
    }
    const validateCode = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入验证码'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        loginName: 'demo',
        password: '888itcast.CN764%...',
        code: ''
      },
      loginRules: {
        loginName: [{ required: true, trigger: 'change', validator: validateloginName }],
        password: [{ required: true, trigger: 'change', validator: validatePassword }],
        code: [{ required: true, trigger: 'change', validator: validateCode }]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined,
      clientToken: '1',
      imgURL: ''
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  created() {
    // 调用验证码
    this.getyanzhengma()
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    // 点击切换验证码
    async getyanzhengma() {
      // 获取随机数
      const random = Math.floor(Math.random() * 10)
      this.clientToken = random
    },
    async logindemo() {
      // 表单验证
      await this.$refs.loginForm.validate()
      // 请求登录接口
      const userInfo = await loginUser({
        ...this.loginForm,
        loginType: '0',
        clientToken: this.clientToken
      })
      console.log(userInfo)
      localStorage.setItem('userInfo', JSON.stringify(userInfo))
      this.$store.state.user.userInfo = userInfo
      this.$store.commit('user/setTokenVuex', userInfo.token)
      this.$router.push('/')
      Cookies.set('clientToken', this.clientToken)
      Cookies.set('code', this.loginForm.code)
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg:#283443;
$light_gray:#fff;
$cursor: #999;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  height: 100px;
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

      caret-color:#121111;
    input {
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;
      color: #999;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid #ccc;
    background:#fff;
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$bg:#2d3a4b;
$dark_gray:#889aa4;
$light_gray:#eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background: url('../../assets/common/background.png') no-repeat;
  overflow: hidden;

  .login-form {
    position: relative;
    top: 200px;
    width: 520px;
    height: 430px;
    max-width: 100%;
    padding: 0 35px 0;
    margin: 0 auto;
    // overflow: hidden;
    background-color: #fff;
    border-radius: 15px;
  }

  .tips {
    font-size: 14px;
    // color: #fff;
    margin-top: -10px;
    color: red;
    // background-color: #2e50e1;
    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
    // background-color: #fff;背景颜色

  }

  .title-container {
    position: relative;
    height: 80px;
    // background: transparent;
    top: -50px;
    z-index: 9999;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
     img{
       width: 96px;
      height: 96px;
     }
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
.el-button{
  height: 52px;
    background: linear-gradient(262deg,#2e50e1,#6878f0);
    opacity: .91;
    border-radius: 8px;
    color: #fff;
    text-shadow: 0 7px 22px #cfcfcf;
}
.yanzhengma{
  position: relative;
  .yanzhengmaImg{
position: absolute;
top: 0;
right: 0;
border: 1px solid #075936;
  }
}
</style>
